﻿using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using Random = UnityEngine.Random;

public class GamePanel : MonoBehaviour
{
    public Button m_btnBack;
    public Button m_btnSetting;

    public GamePlayerInfo[] playersinfo;
    public GameObject m_qipan;
    public GameObject m_qipan_kuaisu;

    private List<Sprite> m_shaiziList = new List<Sprite>();

    private int m_curshaizi = 0;

    private int m_optIndex = 0;

    private int m_maxPlayer = 0;

    private bool m_isGameStart = false;
    // Start is called before the first frame update
    void Start()
    {
        m_btnBack.onClick.AddListener(() =>
        {
            AudioManager.Instance.OnPlayClick();
            this.gameObject.SetActive(false);
            EventCenter.BroadCast(EventType.On_OPEN_PANEL,(object)2);
        });
        m_btnSetting.onClick.AddListener(() =>
        {
            AudioManager.Instance.OnPlayClick();
            EventCenter.BroadCast(EventType.On_OPEN_PANEL,(object)5);
        });
        
        OnInitListen();
    }

    private void OnEnable()
    {
        if (LocalData.Instance.EnterGameType == 1)
        {
            m_qipan.gameObject.SetActive(false);
            m_qipan_kuaisu.gameObject.SetActive(true);
        }
        else
        {
            m_qipan.gameObject.SetActive(true);
            m_qipan_kuaisu.gameObject.SetActive(false);
        }
        
        playersinfo[2].gameObject.SetActive(LocalData.Instance.EnterGame <= 1);
        playersinfo[3].gameObject.SetActive(LocalData.Instance.EnterGame <= 1);
        if (LocalData.Instance.EnterGame <= 1)
        {
            m_maxPlayer = 4;
        }
        else
        {
            m_maxPlayer = 2;
        }

        for (int i = 0; i < playersinfo.Length; i++)
        {
            playersinfo[i].OnInit();
            if (i <= 0)
            {
                playersinfo[i].m_txtName.text = LocalData.Instance.OnSetOrGetPlayerName();
                string head = LocalData.Instance.OnSetPlayerHead();
                playersinfo[i].m_imgHead.sprite = Resources.Load<Sprite>("Image/Sprite/" + head);
            }
            else
            {
                playersinfo[i].m_txtName.text = LocalData.Instance.OnGetRandomName();
                playersinfo[i].m_imgHead.sprite = Resources.Load<Sprite>("Image/Sprite/touxiang"+Random.Range(1,5));
            }
        }
        
        m_optIndex = 0;
        m_isGameStart = true;
        OnStartGame();
    }

    public void OnDisable()
    {
        m_isGameStart = false;
        for (int i = 0; i < playersinfo.Length; i++)
        {
            playersinfo[i].OnInit();
        }
    }

    public void OnInitListen()
    {
        EventCenter.AddListener(EventType.Game_OPT_END, OnPlayerOPTEnd);
        EventCenter.AddListener(EventType.Game_Move_END, OnPlayerMoveEnd);
    }
    
    private void OnStartGame()
    {
        if (!m_isGameStart)
        {
            return;
        }

        playersinfo[m_optIndex].StartOPT(m_optIndex==0);
    }

    //用户操作结束通知
    public void OnPlayerOPTEnd()
    {
        if (!m_isGameStart)
        {
            return;
        }
        playersinfo[m_optIndex].OnDOMovePos();
    }
    //用户移动结束
    public void OnPlayerMoveEnd()
    {
        if (!m_isGameStart)
        {
            return;
        }
        m_optIndex += 1;
        if (m_optIndex > (m_maxPlayer - 1))
        {
            m_optIndex = 0;
        }

        OnStartGame();
    }
    
    
    private void OnDestroy()
    {
        EventCenter.RemoveListener(EventType.Game_OPT_END, OnPlayerOPTEnd);
        EventCenter.RemoveListener(EventType.Game_Move_END, OnPlayerMoveEnd);
    }
}
